<!--设置模块唯一ID-->
<div id="rlzy_zuzhijigou">
<!--标题、面包屑-->
<section class="content-header">
    <h1>
        组织机构
    </h1>
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> 首页</a></li>
        <li>人力资源</li>
        <li class="active">组织机构管理</li>
    </ol>
</section>
<!--列表-->
<section class="content">
    <div class="form-horizontal">
        <div class="form-group">
            <div class="col-sm-3 u_userList">
                <dl>
                    <dt>
                        <i class="fa fa-fw fa-group"></i>组织结构树
                    </dt>
                    <dd>
                        <input class="form-control" type="text" data-plugin="orgTreeSearch"/>
                    </dd>
                </dl>
                <div data-plugin="orgTree" style="height: 400px;overflow: hidden;"></div>
            </div>

            <div class="col-sm-9 u_contextWrap">
            </div>
        </div>
    </div>
</section>

<script type="text/template" data-for="tp_editor">
</script>

<script>
    USEN(function () {
        //模块ID(全系统唯一)
        var id = "#rlzy_zuzhijigou";
        var lv1 = "rlzy_zuzhijigou_level1";
        var lv2 = "rlzy_zuzhijigou_level2";
        var lv3 = "rlzy_zuzhijigou_level3";
        var lv4 = "rlzy_zuzhijigou_level4";
        var lv5 = "rlzy_zuzhijigou_level5";

        //定义页面变量
        var $el = $(id);
        var $container = $el.find('.u_contextWrap:first');
        var base = USEN.CONST.baseUrl;
        var parent_node_id = undefined;

        //初始化用户树结构
        var $orgTree = $el.find("[data-plugin=orgTree]").on("changed.jstree", function (e, data) {
            if (data.selected.length && data.action == 'select_node') {
                var type = data.node.type;
                if(data.node.id == '1'){
                    USEN.fill({
                        selector:$container,
                        url:base + USEN.router[lv1].url,
                        param:{id:data.node.id},
                        success:function(o){
                            console.log('加载一级部门完毕');
                        }
                    })
                }else if(type == 'lv1'){
                    USEN.fill({
                        selector:$container,
                        url:base + USEN.router[lv2].url,
                        param:{id:data.node.id},
                        success:function(o){
                            console.log('加载下属部门完毕');
                        }
                    })
                }else if(type == 'lv2'){
                    USEN.fill({
                        selector:$container,
                        url:base + USEN.router[lv3].url,
                        param:{id:data.node.id},
                        success:function(o){
                            console.log('加载职位完毕');
                        }
                    })
                }else if(type == 'lv3'){
                    USEN.fill({
                        selector:$container,
                        url:base + USEN.router[lv4].url,
                        param:{id:data.node.id},
                        success:function(o){
                            console.log('加载人员完毕');
                        }
                    })
                }else if(type == 'lv4'){
                    USEN.fill({
                        selector:$container,
                        url:base + USEN.router[lv5].url,
                        param:{id:data.node.id},
                        success:function(o){
                            console.log('加载个人信息完毕');
                        }
                    })
                }else{

                }
            }

        }).on('ready.jstree',function(data){
            //插件加载完成后设置默认加载根节点
            if(!_.isUndefined(parent_node_id)) $orgTree.jstree('select_node',parent_node_id);
        }).jstree({
            'core': {
                'themes': {
                    'name': 'proton'
                },
                data: {
                    url: function(node) {
                        switch (node.id){
                            case '#' : return './simdata/common_org_tree_root_action.json';break;
                            case '1' : return './simdata/common_org_tree_level1_action.json';break;
                            case '4' :
                            case '5' :
                            case '6' :
                                return './simdata/common_org_tree_level2_action.json';break;
                            case '7' :
                            case '8' :
                            case '9' :
                                return './simdata/common_org_tree_level3_action.json';break;
                            case '10' :
                            case '11' :
                            case '12' :
                                return './simdata/common_org_tree_level4_action.json';break;
                        }
                    },
                    data:function(node){
                        return {
                            v : new Date().getTime()
                        }
                    },
                    success:function(data){
                        //这有根节点存储根节点ID
                        if(data[0].type == 'org'){
                            parent_node_id = data[0].id;
                        }
                    }
                }
            },
            "plugins": [ "wholerow" , "types" , "search"],
            "types": {
                "lv1": {
                    "icon": "fa fa-fw fa-group"
                },
                "lv2": {
                    "icon": "fa fa-fw fa-male"
                },
                "lv3": {
                    "icon": "fa fa-fw fa-user"
                },
                "lv4": {
                    "icon": "fa fa-fw fa-odnoklassniki"
                }
            }
        });

        //绑定搜索框事件
        $el.find("[data-plugin=orgTreeSearch]").on("input propertychange", function () {
            $orgTree.jstree(true).search($(this).val());
        })

    });
</script>
</div>